﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Dao;
using Entidad;

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            HttpCookie cookie = Request.Cookies["login"];
            if (cookie != null)
            {
                dlTipoUsuario.SelectedValue = cookie["tipo"].ToString();
                txtLegajo.Text = cookie["legajo"].ToString();
            }
        }
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            SeguridadDao seg = new SeguridadDao();
            Usuario u = new Usuario();
            if ((u = seg.ValidarUsuario(dlTipoUsuario.SelectedValue.ToString(), int.Parse(txtLegajo.Text), txtPass.Text.ToString())).activo)
            {
                Session["login"] = u;
                if (cbGuardarSession.Checked)
                {
                    HttpCookie mycookie = new HttpCookie("login");
                    mycookie.Expires = DateTime.Now.AddDays(1.5);
                    mycookie["tipo"] = u.tipoUsuario;
                    mycookie["legajo"] = Convert.ToString(u.legajo);
                    Response.Cookies.Add(mycookie);
                }
                Response.Redirect("Default.aspx");
            }
            else
            {
                Response.Write("<script>alert('¡Usuario Invalido! Verifique el legajo y/o contraseña ingresadas.');</script>");
            }


        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Write("<script>printDiv('login_print');</script>'");
    }
}